/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.client;

#if neko

import haxe.remoting.Context;
import swhx.Window;
import swhx.Flash;
import neko.FileSystem;
import swhx.Application;
import swhx.Connection;

import com.chasekernan.hxnova.client.plugins.PluginFileManager;

typedef ComObject = {
    var name    : String;
    var value   : Dynamic;
}

class SwhxBase {
    
    public static var WINDOW_WIDTH      = 1000;
    public static var WINDOW_HEIGHT     = 700;
    public static var TITLE             = "HxNova";
    public static var UI_SWF            = "ui.swf";
    
    private static var INSTANCE         : SwhxBase;
    
    private var window                  : Window;
    private var flash                   : Flash;
	private var context                 : Context;
    private var connection              : Connection;
    
    
    public static function getInstance() : SwhxBase {
        if (INSTANCE == null) {
			try {
				INSTANCE = new SwhxBase();
				
				//initialize a few other classes
				PluginFileManager.getInstance();
			} catch (e : Dynamic) {
				systools.Dialogs.message("Error", 
						"Error while initializing: " + e, true);
			}
        }
        
        return INSTANCE;
    }
    
    
    private function new() {
        Application.init();
        
        window                  = new Window(TITLE, WINDOW_WIDTH, 
                                                    WINDOW_HEIGHT);
        window.maximizeIcon     = true;
        window.resizable        = true;
        
        setupContext();
        
        flash                   = new Flash(window, context);
        flash.setAttribute("src", UI_SWF);
        
        var me                  = this;
        flash.onConnected       = function() {
            me.connection       = Connection.flashConnect(me.flash);
            me.window.maximized = false;
        };
        
        flash.start();
        
        window.onRightClick     = function() {
            try {
                me.connection.PluginApi.onRightClick.call([]);
            } catch (e : Dynamic) {
                
            }
            
            return false;
        };
        
        window.visible          = true;
        //need to do this as there's a bug in the flash 10 player
        window.maximized        = true;
    }
    
    public function run() {
        Application.loop();
        Application.cleanup();
    }
    
    public inline function getWindow() : Window {
        return window;
    }
    
    public inline function getFlash() : Flash {
        return flash;
    }
    
    public inline function getContext() : Context {
        return context;
    }
    
    public inline function getConnection() : Connection {
        return connection;
    }
    
    public function addCommunicationObject(name : String, value : Dynamic) {
        context.addObject(name, value);
    }
    
    public function addCommunicationObjects(objects : Array<ComObject>) {
        for (obj in objects) addCommunicationObject(obj.name, obj.value);
    }
    
    public function loadTextFile(path : String) : String {
        try {
            return neko.io.File.getContent(path);
        } catch (e : Dynamic) {
            systools.Dialogs.message("Error", 
                                     "Failed to load file: " + path + ". " + e,
                                     true);
            return "";
        }
    }
    
    public function showMessage(title : String, message : String) {
        systools.Dialogs.message(title, message, false);
    }
    
    private inline function setupContext() {
        context = new Context();
        addCommunicationObject("SwhxBase", this);
    }
}

#end